clearvars -except Globaloption option

%% Saving parameter estimates to automatically update the paper
fID = fopen(['../../results/tables_main/UK/post1946/TablePsi_post.tex'], 'w');

%% Baseline Results

loaddata_4CIjoint_annual;

% Psi
Psichar = ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j'];
namebegin = '\\newcommand{\\postPsi';
nameend1 = '}{%4.2f}\n';
nameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [namebegin Psichar(ii) Psichar(jj) nameend1], Psi(ii, jj));
    end

end

% maxeig and R2
fprintf(fID, '\\newcommand{\\Postmaxeig}{%4.3f}\n', max(abs(eig(Psi))));
fprintf(fID, '\\newcommand{\\PostRsqra}{%4.1f}\n', 100 * R2(1));
fprintf(fID, '\\newcommand{\\PostRsqrb}{%4.1f}\n', 100 * R2(2));
fprintf(fID, '\\newcommand{\\PostRsqrc}{%4.1f}\n', 100 * R2(3));
fprintf(fID, '\\newcommand{\\PostRsqrd}{%4.1f}\n', 100 * R2(4));
fprintf(fID, '\\newcommand{\\PostRsqre}{%4.1f}\n', 100 * R2(5));
fprintf(fID, '\\newcommand{\\PostRsqrg}{%4.1f}\n', 100 * R2(6));
fprintf(fID, '\\newcommand{\\PostRsqrh}{%4.1f}\n', 100 * R2(7));
fprintf(fID, '\\newcommand{\\PostRsqrj}{%4.1f}\n', 100 * R2(9));

% T stat
Tstatchar = Psichar;
tnamebegin = '\\newcommand{\\PostTstatt';
tnameend1 = '}{%4.2f}\n';
tnameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [tnamebegin Tstatchar(ii) Tstatchar(jj) tnameend1], tstat(ii, jj));
    end

end

% Sig
Sigchar = Psichar;
snamebegin = '\\newcommand{\\PostSig';
snameend1 = '}{%4.2f}\n';
snameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [snamebegin Sigchar(ii) Sigchar(jj) snameend1], 100 * Sig(ii, jj));
    end

end

% X2 mean
fprintf(fID, '\\newcommand{\\PostVarMeana}{%4.2f}\n', 100 * pi0);
fprintf(fID, '\\newcommand{\\PostVarMeanb}{%4.2f}\n', 100 * y0nom_1);
fprintf(fID, '\\newcommand{\\PostVarMeanc}{%4.2f}\n', 100 * yspr0);
fprintf(fID, '\\newcommand{\\PostVarMeand}{%4.2f}\n', 100 * x0);
fprintf(fID, '\\newcommand{\\PostVarMeane}{%4.2f}\n', 100 * mean(divgrm));
fprintf(fID, '\\newcommand{\\PostVarMeanf}{%4.2f}\n', mean(exp(pdm)));
fprintf(fID, '\\newcommand{\\PostVarMeanh}{%4.2f}\n', 100 * mean(deltalogtau));
fprintf(fID, '\\newcommand{\\PostVarMeani}{%4.2f}\n', 100 * mean((taxrevgdp)));
fprintf(fID, '\\newcommand{\\PostVarMeanj}{%4.2f}\n', 100 * mean(deltalogg));
fprintf(fID, '\\newcommand{\\PostVarMeank}{%4.2f}\n', 100 * mean((spendgdp)));

load MAT/UKpostpara.mat
load MAT/benchmark_UK_30.mat

fprintf(fID, '\\newcommand{\\Postkzerox}{%4.2f}\n', k0x);
fprintf(fID, '\\newcommand{\\Postkonex}{%4.2f}\n', k1x);
fprintf(fID, '\\newcommand{\\Postmeanpvsurplus}{%4.2f}\n', mean(s));
fprintf(fID, '\\newcommand{\\Postmeansurpluszeroz}{%4.2f}\n', mean(upper));
fprintf(fID, '\\newcommand{\\Postmeanpvsurpluspct}{%4.2f}\n', 100 * mean(s));
fprintf(fID, '\\newcommand{\\Postmeansurpluszerozpct}{%4.2f}\n', 100 * mean(upper));
fprintf(fID, '\\newcommand{\\Postukpdgdp}{%4.2f}\n', mean(exp(pxbar)));

fprintf(fID, '\\newcommand{\\Postuksurplus}{%4.2f}\n', mean(taxrevgdp - spendgdp));
fprintf(fID, '\\newcommand{\\Postukdebtgdp}{%4.2f}\n', mean(gdebt));
fprintf(fID, '\\newcommand{\\Postuksurpluspct}{%4.2f}\n', 100 * mean(taxrevgdp - spendgdp));
fprintf(fID, '\\newcommand{\\Postukdebtgdppct}{%4.2f}\n', 100 * mean(gdebt));
fprintf(fID, '\\newcommand{\\Postukgdpreturn}{%4.2f}\n', 100 * mean(gdpreturn));

fprintf(fID, '\\newcommand{\\Postukpvstodebt}{%4.2f}\n', 100 * nanmean(s) / nanmean(gdebt));
A = corrcoef([gdebt(2:end) s'], 'rows', 'pairwise');

fprintf(fID, '\\newcommand{\\Postukcorrpvsdebt}{%4.2f}\n', A(2));
fprintf(fID, '\\newcommand{\\Postukuppertodebt}{%4.2f}\n', 100 * mean(upper) / nanmean(gdebt));


load MAT/debtinVAR_break_UK;
fprintf(fID, '\\newcommand{\\upperUKpostdebtVAR}{%4.2f}\n', upper);
fprintf(fID, '\\newcommand{\\upperUKpostdebtVARpct}{%4.2f}\n', upper * 100);
fprintf(fID, '\\newcommand{\\smeanUKpostdebtVAR}{%4.2f}\n', mean(s));
fprintf(fID, '\\newcommand{\\smeanUKpostdebtVARpct}{%4.2f}\n', mean(s) * 100);
fprintf(fID, '\\newcommand{\\postukcorrpvscydebtdebtVAR}{%4.2f}\n', corr(gdebt(2:end), s'));
fprintf(fID, '\\newcommand{\\postukpvscytodebtdebtVAR}{%4.2f}\n', 100 * nanmean(s) / nanmean(gdebt));
fprintf(fID, '\\newcommand{\\postukuppertodebtdebtVAR}{%4.2f}\n', 100 * upper / nanmean(gdebt));


% 
load MAT/UKpostpara_20.mat;
fprintf(fID, '\\newcommand{\\upperUKposttwenty}{%4.2f}\n', upper);
fprintf(fID, '\\newcommand{\\upperUKposttwentypct}{%4.2f}\n', upper * 100);
fprintf(fID, '\\newcommand{\\smeanUKposttwenty}{%4.2f}\n', mean(s));
fprintf(fID, '\\newcommand{\\smeanUKposttwentypct}{%4.2f}\n', mean(s) * 100);
fprintf(fID, '\\newcommand{\\postukcorrpvsdebttwenty}{%4.2f}\n', corr(gdebt(2:end), s'));
fprintf(fID, '\\newcommand{\\postukpvstodebttwenty}{%4.2f}\n', 100 * nanmean(s) / nanmean(gdebt));
fprintf(fID, '\\newcommand{\\postukuppertodebttwenty}{%4.2f}\n', 100 * upper / nanmean(gdebt));


%% Close File
fclose(fID); 


